package org.spotem.common;

public class BaseException extends Exception {

    private String details;
    private String localizedMessage;

    public BaseException() {
    }

    public BaseException(String message) {
        super(message);
    }

    public BaseException(Throwable cause) {
        super(cause);
    }

    public BaseException(String message, Throwable cause) {
        super(message, cause);
    }

    public String getDetails() {
        return details;
    }

    @SuppressWarnings("unchecked")
    public <T extends BaseException> T setDetails(String details) {
        this.details = details;
        return (T) this;
    }

    @Override
    public String getLocalizedMessage() {
        return localizedMessage == null? getMessage(): localizedMessage;
    }

    @SuppressWarnings("unchecked")
    public <T extends BaseException> T setLocalizedMessage(String localizedMessage) {
        this.localizedMessage = localizedMessage;
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    public <T extends BaseException> T setLocalizedMessage(String localizedMessage, BaseException cause) {
        this.localizedMessage = localizedMessage;
        String causeMsg = cause.getLocalizedMessage();
        if(causeMsg != null && !causeMsg.isEmpty()) {
            this.localizedMessage += ": " + causeMsg;
        }
        return (T) this;
    }
}
